package com.shensi.adminCollect.domain.station.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shensi.adminCollect.common.annotation.Excel;
import com.shensi.common.core.domain.BaseEntity;
import com.shensi.common.core.domain.entity.SysEnterprise;
import com.shensi.common.core.domain.entity.SysStation;
import com.shensi.adminCollect.common.utils.LocalDateTimeUtil;
import com.shensi.adminCollect.domain.station.form.StaffForm;
import com.shensi.common.core.domain.entity.SysUser;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

@Setter
@Getter
@ApiModel("员工")
public class StaffVO extends BaseEntity {

    /** 用户ID */
    @ApiModelProperty("用户序号")
    @Excel(name = "用户序号", cellType = Excel.ColumnType.NUMERIC, prompt = "用户编号")
    private Long userId;


    /** 用户账号 */
    @ApiModelProperty("登录名称")
    @Excel(name = "登录名称")
    private String userName;

    /** 用户昵称 */
    @ApiModelProperty("用户名称")
    @Excel(name = "用户名称")
    private String nickName;

    /** 用户邮箱 */
    @ApiModelProperty("用户邮箱")
    @Excel(name = "用户邮箱")
    private String email;

    /** 手机号码 */
    @ApiModelProperty("手机号码")
    @Excel(name = "手机号码")
    private String phonenumber;

    /** 用户性别 */
    @ApiModelProperty("用户性别")
    @Excel(name = "用户性别", readConverterExp = "0=男,1=女,2=未知")
    private String sex;

    /** 用户头像 */
    @ApiModelProperty("用户头像")
    private String avatar;

    /** 密码 */
    @ApiModelProperty("密码")
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String password;

    /** 盐加密 */
    @ApiModelProperty("盐加密")
    private String salt;

    /** 帐号状态（0正常 1停用） */
    @ApiModelProperty("帐号状态")
    @Excel(name = "帐号状态", readConverterExp = "0=正常,1=停用")
    private String status;

    /** 删除标志（0代表存在 2代表删除） */
    private String delFlag;

    /** 最后登录IP */
    @Excel(name = "最后登录IP", type = Excel.Type.EXPORT)
    private String loginIp;

    /** 最后登录时间 */
    @Excel(name = "最后登录时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss", type = Excel.Type.EXPORT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private LocalDateTime loginDate;

    @Excel(name = "等级", type = Excel.Type.EXPORT)
    private String userLevel;

    /** 油企id */
    @ApiModelProperty("油企id")
    @Excel(name = "油企id", type = Excel.Type.IMPORT)
    private Long enterpriseId;

    /** 油企id */
    @Excel(name = "所属油企id", type = Excel.Type.EXPORT)
    @ApiModelProperty(hidden = true)
    private SysEnterprise enterprise;

    /** 油站ID */
    @Excel(name = "油站id", type = Excel.Type.IMPORT)
    @ApiModelProperty("油站id")
    private Long stationId;

    @Excel(name = "油站名称", type = Excel.Type.EXPORT)
    @ApiModelProperty("油站名称")
    private String stationName;

    /** 油站 */
    @Excel(name = "所属油站id", type = Excel.Type.EXPORT)
    @ApiModelProperty(hidden = true)
    private SysStation stationInfo;


    /** 岗位组 */
    private Long[] postIds;

    /** 枪号，数据示例：2,3,5 */
    @Excel(name = "枪号", type = Excel.Type.EXPORT)
    @ApiModelProperty("枪号集合字符串")
    private String gun;

    /** 枪号集合 */
    @Excel(name = "枪号集合", type = Excel.Type.EXPORT)
    @ApiModelProperty("枪号集合")
    private List<String> gunList;

    /** 推送通知给加油员的开关1开启 */
    @Excel(name = "推送通知给加油员的开关1开启", type = Excel.Type.EXPORT)
    @ApiModelProperty("推送通知给加油员的开关1开启")
    private String empPushFlag;

    /** 发送空订单0不发送1发送 */
    @Excel(name = "发送空订单0不发送1发送", type = Excel.Type.EXPORT)
    @ApiModelProperty("发送空订单0不发送1发送")
    private String nullSendFlag;

    /** 发送的加油员id */
    @Excel(name = "发送的加油员id", type = Excel.Type.EXPORT)
    @ApiModelProperty("发送的加油员id列表字符串")
    private String pushUserIds;

    @ApiModelProperty("发送的加油员id列表")
    private List<String> idList;

    /** 接收通知的openId */
    @Excel(name = "接收通知的openId", type = Excel.Type.EXPORT)
    private String pushOpenid;

    public static StaffVO of(StaffForm staffForm){
        if (staffForm == null) {
            return null;
        }
        StaffVO staffVO = new StaffVO();
        staffVO.setEnterpriseId(staffForm.getEnterpriseId());
        staffVO.setStationId(staffForm.getStationId());
        staffVO.setNickName(staffForm.getNickName());
        staffVO.setUserName(staffForm.getUserName());
        staffVO.setPhonenumber(staffForm.getPhonenumber());
        return staffVO;
    }

    public static StaffVO of(SysUser sysUser){
        if (sysUser == null) {
            return null;
        }
        StaffVO staffVO = new StaffVO();
        staffVO.setUserId(sysUser.getUserId());
        staffVO.setUserName(sysUser.getUserName());
        staffVO.setNickName(sysUser.getNickName());
        staffVO.setEmail(sysUser.getEmail());
        staffVO.setPhonenumber(sysUser.getPhonenumber());
        staffVO.setSex(sysUser.getSex());
        staffVO.setAvatar(sysUser.getAvatar());
        staffVO.setPassword(sysUser.getPassword());
        staffVO.setSalt(sysUser.getSalt());
        staffVO.setStatus(sysUser.getStatus());
        staffVO.setDelFlag(sysUser.getDelFlag());
        staffVO.setLoginIp(sysUser.getLoginIp());
        if (sysUser.getLoginDate() != null) {
            staffVO.setLoginDate(LocalDateTimeUtil.parseLdt(sysUser.getLoginDate()));
        }
        staffVO.setUserLevel(sysUser.getUserLevel());
        staffVO.setEnterpriseId(sysUser.getEnterpriseId());
        staffVO.setEnterprise(sysUser.getEnterprise());
        staffVO.setStationId(sysUser.getStationId());
        staffVO.setStationName(sysUser.getStationName());
        staffVO.setStationInfo(sysUser.getStationInfo());
        staffVO.setPostIds(sysUser.getPostIds());
        staffVO.setGun(sysUser.getGun());
        String gunStr = sysUser.getGun();
        if (StringUtils.isBlank(gunStr)) {
            staffVO.setGunList(Collections.emptyList());
        }else{
            String[] split = gunStr.split(",");
            List<String> longs = Arrays.stream(split).map(Long::parseLong)
                    .sorted().map(String::valueOf).collect(Collectors.toList());
            staffVO.setGunList(longs);
        }
        staffVO.setEmpPushFlag(sysUser.getEmpPushFlag());
        staffVO.setNullSendFlag(sysUser.getNullSendFlag());

        if(StringUtils.isNotBlank(sysUser.getPushUserIds())){
            staffVO.setIdList(Arrays.asList(sysUser.getPushUserIds().split(",")));
        }else {
            staffVO.setIdList(Collections.emptyList());
        }
        //staffVO.setPushUserIds(sysUser.getPushUserIds());

        staffVO.setPushOpenid(sysUser.getPushOpenid());
        staffVO.setCreateName(sysUser.getCreateName());
        staffVO.setLevelId(sysUser.getLevelId());
        staffVO.setPageNumLimit(sysUser.getPageNumLimit());
        staffVO.setPageSizeLimit(sysUser.getPageSizeLimit());
        staffVO.setStationIdList(sysUser.getStationIdList());
        staffVO.setSearchValue(sysUser.getSearchValue());
        staffVO.setCreateBy(sysUser.getCreateBy());
        staffVO.setCreateTime(sysUser.getCreateTime());
        staffVO.setUpdateBy(sysUser.getUpdateBy());
        staffVO.setUpdateTime(sysUser.getUpdateTime());
        staffVO.setRemark(sysUser.getRemark());
        staffVO.setBeginTime(sysUser.getBeginTime());
        staffVO.setEndTime(sysUser.getEndTime());
        staffVO.setParams(sysUser.getParams());
        return staffVO;
    }

    public static SysUser toSysUser(StaffVO staffVO){
        if (staffVO == null) {
            return null;
        }
        SysUser sysUser = new SysUser();
        sysUser.setEmpPushFlag(staffVO.getEmpPushFlag());
        sysUser.setNullSendFlag(staffVO.getNullSendFlag());

        sysUser.setIdList(staffVO.getIdList());

        if(CollectionUtils.isNotEmpty(sysUser.getIdList())){
            List<String> idList = sysUser.getIdList();
            StringJoiner joiner = new StringJoiner(",");
            for (String s : idList) {
                joiner.add(s);
            }
            sysUser.setPushUserIds(joiner.toString());
        }else{
            sysUser.setPushUserIds(null);
        }
        //sysUser.setPushUserIds(staffVO.getPushUserIds());



        sysUser.setPushOpenid(staffVO.getPushOpenid());

        sysUser.setGunList(staffVO.getGunList());
        //sysUser.setGun(staffVO.getGun());
        if(CollectionUtils.isNotEmpty(sysUser.getGunList())){
            List<String> gunList = sysUser.getGunList();
            StringJoiner joiner = new StringJoiner(",");
            for (String s : gunList) {
                joiner.add(s);
            }
            sysUser.setGun(joiner.toString());
        }else{
            sysUser.setGun(null);
        }

        sysUser.setUserLevel(staffVO.getUserLevel());
        sysUser.setStationId(staffVO.getStationId());
        sysUser.setStationInfo(staffVO.getStationInfo());
        sysUser.setStationName(staffVO.getStationName());
        sysUser.setEnterpriseId(staffVO.getEnterpriseId());
        sysUser.setEnterprise(staffVO.getEnterprise());
        sysUser.setUserId(staffVO.getUserId());
        sysUser.setNickName(staffVO.getNickName());
        sysUser.setUserName(staffVO.getUserName());
        sysUser.setEmail(staffVO.getEmail());
        sysUser.setPhonenumber(staffVO.getPhonenumber());
        sysUser.setSex(staffVO.getSex());
        sysUser.setAvatar(staffVO.getAvatar());
        sysUser.setPassword(staffVO.getPassword());
        sysUser.setSalt(staffVO.getSalt());
        sysUser.setStatus(staffVO.getStatus());
        sysUser.setDelFlag(staffVO.getDelFlag());
        sysUser.setLoginIp(staffVO.getLoginIp());
        if (staffVO.getLoginDate() != null) {
            sysUser.setLoginDate(LocalDateTimeUtil.parseDate(staffVO.getLoginDate()));
        }
        sysUser.setPostIds(staffVO.getPostIds());
        sysUser.setCreateName(staffVO.getCreateName());
        sysUser.setLevelId(staffVO.getLevelId());
        sysUser.setPageNumLimit(staffVO.getPageNumLimit());
        sysUser.setPageSizeLimit(staffVO.getPageSizeLimit());
        sysUser.setStationIdList(staffVO.getStationIdList());
        sysUser.setSearchValue(staffVO.getSearchValue());
        sysUser.setCreateBy(staffVO.getCreateBy());
        sysUser.setCreateTime(staffVO.getCreateTime());
        sysUser.setUpdateBy(staffVO.getUpdateBy());
        sysUser.setUpdateTime(staffVO.getUpdateTime());
        sysUser.setRemark(staffVO.getRemark());
        sysUser.setBeginTime(staffVO.getBeginTime());
        sysUser.setEndTime(staffVO.getEndTime());
        sysUser.setParams(staffVO.getParams());
        return sysUser;
    }

}
